package com.example.wms.utils;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {

    //联网工具类对象，主要是联网进行一些操作
    public static OkHttpUtils instance;
    public static String URL = "http://192.168.138.92:8089/";

    //1 创建一个OkHttp对象
    private static OkHttpClient client;

    public OkHttpUtils() {
        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        Log.d("==w", "verify: 设置默认验证通过");
                        return true;
                    }
                })
                .build();
    }

    //网络请求的单例模式 一般在有3个线程以上时应用...
    public static OkHttpUtils getInstance() {
        if (instance == null) {
            synchronized (OkHttpUtils.class) {
                if (instance == null) {
                    instance = new OkHttpUtils();
                }
            }
        }
        return instance;
    }

    /*
     * 登陆
     * */
    public void Login(String account, String password, Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", account);
            jsonObject.put("password", password);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");

        RequestBody requestBody = RequestBody.Companion.create(String.valueOf(jsonObject), mediaType);
        Request requestPost = new Request.Builder()
                .url(URL + "login1")

                .post(requestBody)
                .build();
        client.newCall(requestPost).enqueue(callback);

    }

    public void getOrder(Callback callback) {
        Request requestPost = new Request.Builder()
                .url(URL + "emsboot/order/app")
                .get()
                .build();
        client.newCall(requestPost).enqueue(callback);
    }

    // 提取 货物的接口
    public void extract(JSONObject jsonObject,Callback callback) {

        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");

        RequestBody requestBody = RequestBody.Companion.create(String.valueOf(jsonObject), mediaType);
        Request requestPost = new Request.Builder()
                .url(URL + "emsboot/order")

                .put(requestBody)
                .build();
        client.newCall(requestPost).enqueue(callback);
    }


}
